package com.stanyung.javasample.json;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.junit.Test;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;

public class FastJsonSample {

	@Test
	public void testObject2Json() {
		List<Person> list = new ArrayList<Person>();
		for (int i = 0; i < 2; i++) {
			Person p = new Person(i, "name" + i);
			list.add(p);
		}
		String json = JSON.toJSONString(list);
		System.out.println(json);
		
		Result result = new Result();
		result.setResult(1);
		result.setList(list);
		json = JSON.toJSONString(result);
		System.out.println(json);
		
		ResultBean<List<Person>> resultBean = new ResultBean<List<Person>>();
		resultBean.setResult(1);
		resultBean.setData(list);
		json = JSON.toJSONString(resultBean);
		System.out.println(json);
	}
	
	
	@Test
	public void testJson2Object() {
		String json = "[{\"id\":0,\"name\":\"name0\"},{\"id\":1,\"name\":\"name1\"}]";
		List<Map> list = (List<Map>)JSON.parse(json);
		for(Map m : list) {
			System.out.println(m.get("id") + ", " + m.get("name"));
		}
		List<Person> plist = (List<Person>)JSON.parseArray(json, Person.class);
		System.out.println(plist);
		
		json  = "{\"list\":[{\"id\":0,\"name\":\"name0\"},{\"id\":1,\"name\":\"name1\"}],\"result\":1}";
		Result result = JSON.parseObject(json, Result.class);
		System.out.println(result);
		
		json = "{\"data\":[{\"id\":0,\"name\":\"name0\"},{\"id\":1,\"name\":\"name1\"}],\"result\":1}";
		
		ResultBean<List<Person>> resultBean = JSON.parseObject(json, new TypeReference<ResultBean<List<Person>>>(){});
		System.out.println(resultBean);
		
	}

	@Test
	public void testJson2ObjectX() {
		String json  = "{\"list\":[{\"id\":0,\"name\":\"name0\"},{\"id\":1,\"name\":\"name1\"}],\"result\":1}";
		ResultX result = JSON.parseObject(json, ResultX.class);
		System.out.println(result);
		Object obj = result.getList();
		List<Map> list = (List<Map>)obj;
		
	}
	
	@Test
	public void testJson2IntegerArray() {
		int[] arr = {12,23,36,48};
		String json = JSON.toJSONString(arr);
		System.out.println(json);
		List<Integer> intList = (List<Integer>)JSON.parse(json);
	}
	
	public static class Result {
		private int result;
		private List<Person> list;
		public int getResult() {
			return result;
		}
		public void setResult(int result) {
			this.result = result;
		}
		public List<Person> getList() {
			return list;
		}
		public void setList(List<Person> list) {
			this.list = list;
		}
	}
	
	public static class ResultX {
		private int result;
		private Object list;
		public int getResult() {
			return result;
		}
		public void setResult(int result) {
			this.result = result;
		}
		public Object getList() {
			return list;
		}
		public void setList(Object list) {
			this.list = list;
		}
	}
	
	public static class ResultBean<T> {
		private int result;
		private T data;
		public int getResult() {
			return result;
		}
		public void setResult(int result) {
			this.result = result;
		}
		public T getData() {
			return data;
		}
		public void setData(T data) {
			this.data = data;
		}
	}

	public static class Person {
		private int id;
		private String name;

		public Person() {
			super();
		}

		public Person(int id, String name) {
			super();
			this.id = id;
			this.name = name;
		}

		public int getId() {
			return id;
		}

		public void setId(int id) {
			this.id = id;
		}

		public String getName() {
			return name;
		}

		public void setName(String name) {
			this.name = name;
		}

	}

}
